package cn.iocoder.yudao.module.wengfu.service.base;

import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import cn.iocoder.yudao.module.wengfu.dal.dataobject.approval.SampleApprovalInfoDO;
import cn.iocoder.yudao.module.wengfu.dal.mysql.approval.SampleApprovalInfoMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import java.util.*;
import cn.iocoder.yudao.module.wengfu.controller.admin.base.vo.*;
import cn.iocoder.yudao.module.wengfu.dal.dataobject.base.SampleBaseInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wengfu.dal.mysql.base.SampleBaseInfoMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wengfu.enums.ErrorCodeConstants.*;

/**
 * 样品基础信息 Service 实现类
 *
 * @author yzs
 */
@Service
@Validated
public class SampleBaseInfoServiceImpl implements SampleBaseInfoService {

    @Resource
    private SampleBaseInfoMapper sampleBaseInfoMapper;

    @Resource
    private BpmProcessInstanceApi processInstanceApi;

    @Resource
    private SampleApprovalInfoMapper sampleApprovalInfoMapper;

    @Resource
    private HySeqService hySeqService;

    /**
     * 瓮福结算样对应的流程定义 KEY
     */
    public static final String SAMPLE_APPROVAL_PROCESS_KEY = "Sample_approval_process";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String createSampleBaseInfo(Long UserId, SampleBaseInfoSaveReqVO createReqVO) {
        //oa流程的推进
        // 插入 OA 请假单
        SampleBaseInfoDO sampleBase = BeanUtils.toBean(createReqVO, SampleBaseInfoDO.class);
        sampleBase.setCreator(UserId.toString());

        // ① 获取下一个序号
        long seq = hySeqService.nextSeq();
        // ② 拼接前缀（不做左补零，直接 hy1、hy2 …）
        String testId = "HY" + seq;
        sampleBase.setTestId(testId);

        //将数据保存到样品基础表
        sampleBaseInfoMapper.insert(sampleBase);


        // 发起 BPM 流程，远程调用BPM模块进行流程驱动
        Map<String, Object> processInstanceVariables = new HashMap<>();
        processInstanceVariables.put("approval_status ", 0);

        String processInstanceId = processInstanceApi.createProcessInstance(UserId,
                        new BpmProcessInstanceCreateReqDTO().setProcessDefinitionKey(SAMPLE_APPROVAL_PROCESS_KEY)
                                .setVariables(processInstanceVariables)
                                .setBusinessKey(String.valueOf(sampleBase.getId())))
                .getCheckedData();




        // 将工作流的编号，更新到 OA 请假单中
        sampleApprovalInfoMapper.updateById(new SampleApprovalInfoDO().setId(sampleBase.getId()).setProcessInstanceId(processInstanceId));
        return testId;
    }


    @Override
    public void updateSampleBaseInfo(SampleBaseInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateSampleBaseInfoExists(updateReqVO.getId());
        // 更新
        SampleBaseInfoDO updateObj = BeanUtils.toBean(updateReqVO, SampleBaseInfoDO.class);
        sampleBaseInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteSampleBaseInfo(Long id) {
        // 校验存在
        validateSampleBaseInfoExists(id);
        // 删除
        sampleBaseInfoMapper.deleteById(id);
    }

    @Override
        public void deleteSampleBaseInfoListByIds(List<Long> ids) {
        // 删除
        sampleBaseInfoMapper.deleteByIds(ids);
        }


    private void validateSampleBaseInfoExists(Long id) {
        if (sampleBaseInfoMapper.selectById(id) == null) {
            throw exception(SAMPLE_BASE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public SampleBaseInfoDO getSampleBaseInfo(Long id) {
        return sampleBaseInfoMapper.selectById(id);
    }

    @Override
    public PageResult<SampleBaseInfoDO> getSampleBaseInfoPage(SampleBaseInfoPageReqVO pageReqVO) {
        return sampleBaseInfoMapper.selectPage(pageReqVO);
    }

}
